<?php

namespace app\admin\controller;

use app\common\model\MemberPointsLog;
use hg\apidoc\annotation as Apidoc;
use hg\apidoc\annotation\Param;
use hg\apidoc\annotation\Query;
use hg\apidoc\annotation\Returned;
use hg\apidoc\annotation\Header;
use app\BaseController;
use think\facade\View;
use think\facade\Db;
use think\response\Json;
/**
 * 会员
 *
 * admin-Member
 *
 * @Apidoc\Title("会员")
 * @Apidoc\Group("api")
 */
class Member extends BaseController
{
    /**
     * 新增
     *
     * admin-Member-add
     */
    public function add()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\Member::create($data['member']);
            return success();
        } else {
            return View::fetch();
        }
    }
    /**
     * 修改
     *
     * admin-Member-update
     */
    public function update()
    {
        if (request()->isPost()) {
            $data = input();
            $data = group_by_table_name($data);
            \app\common\model\Member::update($data['member']);
            return success();
        } else {
            $data = \app\common\model\Member::find(input('member__id'));
            $r = [];
            foreach ($data->getData() as $key => $val) {
                $r['member__' . $key] = $val;
            }
            View::assign('r', $r);
            return View::fetch();
        }
    }
    /**
     * 删除
     *
     * admin-Member-delete
     */
    public function delete()
    {
        \app\common\model\Member::destroy(input('member__id'));
        return success();
    }
    /**
     * 列表
     *
     * admin-Member-list
     */
    public function list()
    {
        if (request()->isPost()) {
            return error('请求方式错误');
        }
        $query = Db::table('db_member');
        if (input('member__create_time_start', '') || input('member__create_time_end', '')) {
            $member__create_time_start = input('member__create_time_start', '', 'search_in');
            $member__create_time_end = input('member__create_time_end', '', 'search_in');
            $member__create_time_start = $member__create_time_start ? strtotime($member__create_time_start) : 0;
            $member__create_time_end = $member__create_time_end ? strtotime($member__create_time_end) + 24 * 60 * 60 - 1 : time();
            $query->where('db_member.create_time', 'between', [$member__create_time_start, $member__create_time_end]);
        }
        if (input('member__id', '')) {
            $query->where('db_member.id', '=', input('member__id', 0, 'intval') );
        }
        if (input('member__name', '') !== '') {
            $query->where('db_member.name', 'like', '%' . input('member__name', '', 'search_in') . '%');
        }
        if (input('member__phone', '') !== '') {
            $query->where('db_member.phone', 'like', '%' . input('member__phone', '', 'search_in') . '%');
        }        
        $query->where('db_member.delete_time', '=', null);
        $query->field(['db_member.id' => 'member__id']);
        $query->field(['db_member.is_mobile_registration' => 'member__is_mobile_registration']);
        $query->field(['db_member.create_time' => 'member__create_time']);
        $query->withAttr('member__create_time', function ($value, $data) {
            return date('Y-m-d H:i:s', $value);
        });
        $query->field(['db_member.name' => 'member__name']);
        $query->field(['db_member.status' => 'status']);
        $query->field(['db_member.phone' => 'member__phone']);
        $query->field(['db_member.chat_end_time' => 'member__chat_end_time']);
        $query->field(['db_member.number_of_free_chats' => 'member__number_of_free_chats']);
        $query->field(['db_member.point_num' => 'member__point_num']);
        $query->field(['db_member.avatar' => 'member__avatar']);
        $query->order('db_member.id', 'desc');
        $data = $query->paginate(input('listRow') ?: 15);
        $data = ['data' => $data->items(), 'paginate' => $data->render()];
        View::assign($data);
        $options = [];
        $member__id_option = \app\common\model\Member::column('name as name, id as id');
        $options['member__id_options'] = $member__id_option;
        View::assign($options);
        return View::fetch();
    }

    public function recharge()
    {
        if(request()->isPost()){
            $data = input();
            $member_id = $data['id'];
            validate([
                'type|类型' => 'require',
                'point|类型' => 'require',
                'remark|类型' => 'require',
            ])->check($data);
            $cost_points = $data['type'] * $data['point'];
            // 积分验证
            $member = \app\common\model\Member::find($member_id);
            $member_point_num = $member['point_num'];
                \app\common\model\Member::update([
                    'id' => $member_id,
                    'point_num' => $member_point_num + $cost_points
                ]);


            $point_log = MemberPointsLog::create([
                'member_id' => $member_id,
                'num' => $cost_points,
                'end_num' => $member_point_num + $cost_points,
                'remarks' => $data['remark']
            ]);
            return success();
        }else{
            $member = \app\common\model\Member::find(input('id'));
            View::assign('member', $member);
            return View::fetch();
        }
    }

    public function view()
    {
        $member = \app\common\model\Member::find(input('id'));
        if(!$member['is_mobile_registration']){
            $log = \app\common\model\MemberLoginLog::where('member_id', input('id'))
                ->order('id asc')->find();
            $ip = $log['ip'];
            $member['ip'] = $ip;
            if(!$member['is_mobile_registration']){
                $visit = DB::connect('www')->table('db_visit')
                    ->where('ip', $ip)->order('first_time', 'asc')->find();
                $member['visit'] = $visit;
            }
        }
        $member['gptRequestLog']= \app\common\model\GptRequestLog::where("member_id",$member->id)->count();
        $member['pushLog']= \app\common\model\PushLog::where("member_id",$member->id)->count();
        $member['memberLoginLog']= \app\common\model\MemberLoginLog::where("member_id",$member->id)->count();
        $member['orders']= \app\common\model\Orders::where("member_id",$member->id)->count();
        $member['memberPointsLog']= \app\common\model\MemberPointsLog::where("member_id",$member->id)->count();
        $member['article']= \app\common\model\Article::where("member_id",$member->id)->count();
        View::assign('r', $member);
        return View::fetch();
    }
    public function set_status()
    {
        $member_id=input("id",0,"intval");
        $status=input("status",0,"intval");
        \app\common\model\Member::update([
            "id"=>$member_id,
            "status"=>$status
        ]);
        return success("操作成功");
    }
    public function vlogin()
    {
        $url = config('front_end.url');
        $random_key = uniqid();
        cache($random_key, input('id'));
        return $url . $random_key;
    }
}
